package org.wuyong.rtc.signalling.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author He Peng
 * @create 2017-12-30 23:37
 * @update 2017-12-30 23:37
 * @updatedesc : 更新说明
 * @see
 */

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        List<String> allows = new ArrayList<>();
        allows.add("http://localhost");
        allows.add("ws://localhost");
        OriginHandshakeInterceptor handshakeInterceptor = new OriginHandshakeInterceptor();
        handshakeInterceptor.setAllowedOrigins(allows);
        stompEndpointRegistry.addEndpoint("/endpointSang")
                    .withSockJS()
                    .setInterceptors(handshakeInterceptor);
    }
}
